package com.example.deeplearningjavafrombug.regex.lesson1;

import org.junit.Test;

public class Client {

    public static void main(String[] args) {

        System.out.println(isValidPhoneNumber1("15084882347"));

        System.out.println(isValidPhoneNumber2("15084882347"));

    }

    public static boolean isValidPhoneNumber1(String number) {
        // 判断是否是 11 位
        if (number.length() != 11) return false;
        // 判断每一位是否全为数字
        for (int i = 0; i < number.length(); i++) {
            if (number.charAt(i) < '0' || number.charAt(i) > '9') return false;
        }
        return true;
    }

    public static boolean isValidPhoneNumber2(String number) {
        return number.matches("\\d{11}");
    }


    @Test
    public void test1() {
        System.out.println("abc".matches("abc")); // 输出为 true
        System.out.println("ab".matches("abc")); // 输出为 false
        System.out.println("ABC".matches("abc")); // 输出为 false
    }

    @Test
    public void test2() {
        // 输出为 true
        System.out.println("1".matches("\\d"));
        // 输出为 true
        System.out.println("a".matches("\\w"));
        // 输出为 true
        System.out.println(" ".matches("\\s"));

    }

    @Test
    public void test3() {
        // 输出都为 true，因为 . 号可以匹配任意字符，也可以理解为 . 号是占位符，不做任何规则判断
        System.out.println("abc".matches("a.c"));
        System.out.println("acc".matches("a.c"));
        System.out.println("adc".matches("a.c"));

        System.out.println("ac".matches("a.c")); // false
    }

    @Test
    public void test4() {
        // 输出为 true，写三遍以匹配三个字符
        System.out.println("abc".matches("..."));
        // 输出为 false，四个占位符无法匹配三个字符
        System.out.println("abc".matches("...."));
        // 输出为 false，两个占位符无法匹配三个字符
        System.out.println("abc".matches("src/main"));
    }

    @Test
    public void test5() {
        System.out.println(isValidPhoneNumber3("15084882347"));
        System.out.println(isValidPhoneNumber4("15084882347"));

    }

    public static boolean isValidPhoneNumber3(String number) {
        // 判断是否是 11 位
        if (number.length() != 11) return false;
        // 判断第一位是否是 1
        if (number.charAt(0) != '1') return false;
        // 判断第二位是否在 3～9 的范围内
        if (number.charAt(1) < '3' || number.charAt(1) > '9') return false;
        // 判断剩余的每一位是否全为数字
        for (int i = 2; i < number.length(); i++) {
            if (number.charAt(i) < '0' || number.charAt(i) > '9') return false;
        }
        return true;
    }

    public static boolean isValidPhoneNumber4(String number) {
        return number.matches("1[3456789]\\d{9}");
    }

    @Test
    public void test6() {
        System.out.println("abc".matches(".*"));
        System.out.println("ab1".matches(".*"));
        System.out.println("abadhwuidhdmad".matches(".*"));
    }

    @Test
    public void test7() {
        // 输出为 true
        System.out.println("xiaojiang@leetcode.com".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));
        System.out.println("xiao+jiang@leetcode.com".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));
        System.out.println("xiao-jiang@leetcode.com".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));
        System.out.println("xiao.jiang@leetcode.com".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));
        System.out.println("xiao.jiang.regex@leetcode.com".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));
        System.out.println("xiao-jiang.regex@leetcode.com".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));
        System.out.println("xiao-jiang.regex@leet-code.com".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));
        System.out.println("xiao-jiang.regex@leet-code.com.cn".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));
        // 输出为 false
        System.out.println("invalid/xiaojiang@leetcode.com".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));
        System.out.println("invalid.xiaojiang@leetcode#com".matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"));

    }


}
